//引入express模块
const express=require('express');
//引入连接池模块
const pool=require('../pool.js');
//console.log(pool);
//创建路由器对象
const r=express.Router();

//查出所有蛋糕的信息
r.get('/allcakes',(req,res)=>{
    let sql='select * from wdm_cake';
    pool.query(sql,[],(error,result)=>{
            if(error) throw error;
            res.send({message: 'ok', code: 200, results: result})
    })
})







//查出列表页中商品 种类family_id=? 的蛋糕的所有信息
r.get('/cakes',(req,res)=>{
    let fid=req.query.fid;
    let sql='select * from wdm_cake where family_id= ?';
    pool.query(sql,[fid],(error,result)=>{
            if(error) throw error;
            res.send({message: 'ok', code: 200, results: result})
    })
})

//每个详情页商品的所有数据
r.get('/detail',(req,res)=>{
    let lid=req.query.lid;
    let sql='select * from wdm_cake where lid=?'
    pool.query(sql,[lid],(error,result)=>{
        if(error) throw error;
        console.log(result);
        res.send({message: 'ok', code: 200, results: result})
})
})
//查出对应蛋糕的种类
r.get('/allcategory',(req,res)=>{
    let lid=req.query.lid;
   //select ename,dname from emp inner join dept on deptId=did;
    let sql='select lid,fname from wdm_cake inner join wdm_cake_family on family_id=fid where lid=?'
    pool.query(sql,[lid],(error,result)=>{
        if(error) throw error;
        console.log(result);
        res.send({message: 'ok', code: 200, results: result})
})
})

//由高到低查询出销量前十的蛋糕的title,img
r.get('/top',(req,res)=>{
    
    let sql='select title,img_sm,img_number,sold_count from wdm_top order by sold_count desc';
    pool.query(sql,[],(error,result)=>{
             console.log(result)
            if(error) throw error;
            res.send({message: 'ok', code: 200, results: result})
    })
})

//商品搜索--模糊查询
r.get("/search",(req,res)=>{
    let key='%'+req.query.keyword+'%'
    let sql="select * from wdm_cake where title like ?"
    pool.query(sql,[key],(err,result)=>{
            if(err) throw err
            res.send(result)
    })
})





//导出路由器对象
module.exports=r;